package com.springboot.huli.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.huli.entity.Qiandao;
import com.springboot.huli.entity.vo.QiandaoVo;
import com.springboot.huli.mapper.QiandaoMapper;
import com.springboot.huli.qpage.QPage;
import com.springboot.huli.service.IQiandaoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.huli.util.Result;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 签到打卡表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-03-29
 */
@Service
public class QiandaoServiceImpl extends ServiceImpl<QiandaoMapper, Qiandao> implements IQiandaoService {


    /**
     * 列表分页
     * @param qPage
     * @param obj
     * @return
     */
    @Override
    public IPage getList(QPage qPage, QiandaoVo obj) {
        IPage iPage = new Page(qPage.getPage(), qPage.getRows());
        List<QiandaoVo> mapList = baseMapper.list(iPage,obj);
        return iPage.setRecords(mapList);
    }

    /**
     * 新增
     * @param obj
     * @return
     */
    @Override
    public Result insert(Qiandao obj) {
        //查看当天用户是否已经签到过
        QiandaoVo qiandao=this.baseMapper.getQiandao(obj.getUserid());
        if(qiandao!=null){
            return Result.reError("你当天已经签到过了，无须重新签到");
        }
        obj.setQddate(new Date());
        this.baseMapper.insert(obj);
        return Result.reOk("签到成功");
    }

    /**
     * 修改
     * @param obj
     * @return
     */
    @Override
    public Result updates(Qiandao obj) {
        QiandaoVo qiandao=this.baseMapper.getQiandao(obj.getUserid());
        if(qiandao==null){
            return Result.reError("请当天先签到，再来签退");
        }
        //查看当天用户是否已经签到过
        QiandaoVo qiantui=this.baseMapper.getQiantui(obj.getUserid());
        if(qiantui!=null){
            return Result.reError("当天你已经签退过了，无须重复签退");
        }
        qiandao.setId(qiandao.getId());
        qiandao.setQtdate(new Date());
        this.baseMapper.updateById(qiandao);
        return Result.reOk("签退成功");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public Result deleteId(Integer id) {
        int isOk=this.baseMapper.deleteById(id);
        if(isOk==1){
            return Result.reOk("删除成功");
        }
        return Result.reError("删除失败");
    }



}
